<template>
  <div class="container">
    <div class="title">新建模板</div>
    <div class="wrap">
      <el-row>
        <el-col :lg="16" :md="20" :sm="24" :xs="24">
          <el-form :model="form" status-icon ref="form" label-width="100px" @submit.native.prevent>
            <el-form-item label="模板名称" prop="title">
              <el-input size="medium" v-model="form.title" placeholder="请填写模板名称"></el-input>
            </el-form-item>
            <el-form-item label="作者" prop="author">
              <el-input size="medium" v-model="form.author" placeholder="请填写作者"></el-input>
            </el-form-item>

            <!-- <el-form-item label="封面" prop="image">
              <el-input
                size="medium"
                v-model="form.image"
                placeholder="请填写封面地址"
              ></el-input>
            </el-form-item> -->
            <el-form-item label="封面">
              <upload-imgs ref="uploadEle3" :rules="rules" :multiple="true" />
              <!-- <div><el-button @click="getValue('uploadEle3')">获取当前图像数据</el-button></div> -->
            </el-form-item>
            <el-form-item label="文章内容">
              <span>插入标题使用$1;插入作者使用$2;</span>
              <tinymce :defaultContent="data" @change="change" upload_url="http://dev.lin.colorful3.com/cms/file/" />
            </el-form-item>
            <el-form-item class="submit">
              <el-button type="primary" @click="submitForm('form')" :loading="loading">保 存</el-button>
              <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import book from '@/model/book'
import moban from '@/model/moban'
import UploadImgs from '@/component/base/upload-image'
import Tinymce from '@/component/base/tinymce'

export default {
  components: {
    UploadImgs,
    Tinymce,
  },
  data() {
    return {
      data: '',
      rules: {
        minWidth: 100,
        minHeight: 100,
        maxSize: 5,
      },
      form: {
        opcode: '11111',
        title: '',
        author: '',
        summary: '',
        image: '',
      },
      loading: false,
    }
  },
  methods: {
    change() {},
    async getValue(name) {
      console.log(await this.$refs[name].getValue())
      // eslint-disable-next-line
      alert('已获取数据, 打印在控制台中')
    },
    async submitForm(formName) {
      try {
        this.loading = true
        const res = await moban.createMoban(this.form)
        this.loading = false
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.$message.success(`${res.message}`)
          this.resetForm(formName)
        }
      } catch (error) {
        this.loading = false
        this.$message.error('图书添加失败，请检测填写信息')
        console.log(error)
      }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;
    border-bottom: 1px solid #dae1ec;
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}
</style>
